﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using HGZP.BLL;
using HGZP.DAL;
using HGZP.Model;
using HGZP.PLL;
namespace SZCB.admin.FoodManager
{
	public partial class EditSubstance : System.Web.UI.Page
	{
	
        protected void Page_Load(object sender, EventArgs e)
		{

            if (IsPostBack == false)
            {
                //删除流程
                if (Request.QueryString["Option"] == "Edit")
                {
                    //string _sGuid = Request.QueryString["Id"];
                    InitSourceData();
                }
                else
                {
                    InitData();
                }
            }

		}


        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["Option"] == "Edit")
            {
                //string _sGuid = Request.QueryString["Id"];
                SaveEdtiData();
            }
            else
            {
                SaveData();
            }

        }

        protected void InitSourceData()
        {
            CFoodSubstanceBLL myCFoodSubstanceBLL = new CFoodSubstanceBLL();
            CFoodSubstance myCFoodSubstance = myCFoodSubstanceBLL.F06_SelectOneEntityByGuid(Request.QueryString["Id"]);
            if (null != myCFoodSubstance)
            {
                txtName.Text = myCFoodSubstance.sSubtanceName;
                txtNutrient.Text = myCFoodSubstance.fNutrient.ToString();
            }
        }

        protected void InitData()
        {
            CSubstanceBLL myCSubstanceBLL = new CSubstanceBLL();
            CSubstance myCSubstance = myCSubstanceBLL.F06_SelectOneEntityByGuid(Request.QueryString["Id"]);
            txtName.Text = myCSubstance.sName;
            txtNutrient.Text = "";

        }

        protected void SaveEdtiData()
        {
            CFoodSubstanceBLL myCFoodSubstanceBLL = new CFoodSubstanceBLL();
            CFoodSubstance myCFoodSubstance = myCFoodSubstanceBLL.F06_SelectOneEntityByGuid(Request.QueryString["Id"]);
            myCFoodSubstance.fNutrient = Convert.ToDecimal(txtNutrient.Text);
            if( ! myCFoodSubstanceBLL.F_Update(myCFoodSubstance))
            {
                string strErrmessage = "数据库更新失败";
                Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('" + strErrmessage + "');</script>");
 
            }

        }

        protected void SaveData()
        {
            CFoodSubstanceBLL myCFoodSubstanceBLL = new CFoodSubstanceBLL();
            CFoodSubstance myCFoodSubstance = new CFoodSubstance();
            CSubstanceBLL myCSubstanceBLL = new CSubstanceBLL();
            CSubstance myCSubstance = myCSubstanceBLL.F06_SelectOneEntityByGuid(Request.QueryString["Id"]);

            myCFoodSubstance.fNutrient = Convert.ToDecimal(txtNutrient.Text);
            myCFoodSubstance.sGuid = MyFunction.GetNewGuid();
            myCFoodSubstance.sSubtanceGuid = Request.QueryString["Id"];
            myCFoodSubstance.sSubtanceName = myCSubstance.sName;
            //myCFoodSubstance.sFoodGuid = 
            if (!myCFoodSubstanceBLL.F_Insert(myCFoodSubstance))
            {
                string strErrmessage = "营养成分含量添加失败";
                Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('" + strErrmessage + "');</script>");

            }

        }

	}
}